import { NodeRoot } from "../Base/NodeRoot";
import { NMNode } from "../Base/NodeGraph";
@NMNode
export class MoveToTarget extends NodeRoot{
    public Node: cc.Node;
    public TargetNode:cc.Node;
    public Speed:number;
    private moveNode:cc.Node;
    private isArrive=false;
    onEnter(){
        this.moveNode=this.Node;
        if(!this.moveNode){
            this.moveNode=this.ownerNode;
        }
        this.isArrive=false;
        console.log("开始移动",this.Node,this.TargetNode);
    }
    onStep(dt){
        let sub=this.TargetNode.position.sub(this.moveNode.position);
        let dis=Math.sqrt(sub.x*sub.x+sub.y+sub.y);
        if(dis>this.Speed*dt){
            this.moveNode.position=this.moveNode.position.add(sub.normalize().scale(cc.v2(this.Speed*dt,this.Speed*dt)));
        }
        else{
            this.moveNode.runAction(cc.moveTo(dis/this.Speed,this.TargetNode.getPosition()));
            this.isArrive=true;
        }
        return this.isArrive;
    }
}